package models

import (
	"common/db/fields"
	"common/rs"
	"common/uuid"
	"database/sql"
	"time"
)

const (
	ObjectStatusPending   = 1 //上传中
	ObjectStatusCommitted = 3 //上传成功
	ObjectStatusDeleting  = 2 //准备删除中
)

type Object struct {
	StreamId         uuid.UUID        `gorm:"column:stream_id;primaryKey;type:BINARY(16);notNull;comment:流id" json:"stream_id"`
	BucketId         uuid.UUID        `gorm:"column:bucket_id;type:BINARY(16);notNull;comment:bucket_id" json:"bucket_id"`
	EncryptObjectKey string           `gorm:"column:encrypt_object_key;index;type:bytes;size:500;notNull;comment:加密后的objectKey" json:"encrypt_object_key"`
	SegmentCount     int              `gorm:"column:segment_count;notNull;default:0;comment:segment数量" json:"segment_count"`
	SegmentSize      int              `gorm:"column:segment_size;notNull;default:0;comment:segment大小限制" json:"segment_size"`
	Status           int8             `gorm:"column:status;notNull;default:0;comment:状态" json:"status"`
	EncryptedKey     []byte           `gorm:"column:encrypted_key;type:bytes;size:500;notNull;comment:加密文件Key" json:"encrypted_key"`
	PlainSize        int64            `gorm:"column:plain_size;notNull;default:0;comment:原始大小" json:"plain_size"`
	Redundancy       rs.Rs            `gorm:"column:redundancy;type:int;notNull;default:0;comment:分片信息" json:"redundancy"`
	ExpireTime       sql.NullTime     `gorm:"column:expire_time;type:time;comment:过期时间" json:"expire_time"`
	CreatedAt        time.Time        `gorm:"column:created_at;type:time;notNull;default:CURRENT_TIMESTAMP" json:"created_at"`
	Inline           bool             `gorm:"column:inline;notNull;default:false" json:"inline"`
	Attribute        fields.Attribute `gorm:"column:attribute;notNull;default:;comment:对象属性" json:"attribute"`

	Segments []Segment `gorm:"foreignKey:stream_id;references:stream_id"`
}

func (o *Object) Expired() bool {
	if o == nil || o.ExpireTime.Valid && !o.ExpireTime.Time.After(time.Now()) {
		return true
	}
	return false
}
